const Net = require('../../common.js')
Page({
  data: {
    pageInput: 2,
    showPageStatus: false,
    info: {},
    phone: '',
    description: '',
    imageUrl: '',
    status: 0
  },
  onShowModal(e) {
    var id = e.currentTarget.id
    Net.data({
      pageInput: id,
      showPageStatus: !Net.data('showPageStatus')
    })
  },
  hidModal() {
    Net.data({
      showPageStatus: !Net.data('showPageStatus')
    })
  },
  onPreviewShop() {
    Net.data({
      status: 0
    })
    Net.PageRoter('_previewStore', '', 'nav')
  },
  onPageInfo() {
    var storeId = wx.getStorageSync('store')
    Net.link('v1/store/' + storeId, {
      id: storeId
    }, function (res) {
      if (res.data.code == 6 || res.data.mag == "Token验证失败") {
        Net.filaModal()
      }
      Net.data({
        info: res.data.data,
        phone: res.data.data.data.telephone,
        description: res.data.data.data.description,
      })
    }, 'GET')
  },
  onLoad: function (options) {
    var _this = this
    _this.onPageInfo()
  },
  /**
   * 获取手机号简介等信息
   */
  onPageContent(e) {
    if (Net.data('pageInput') == 1) {
      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
      if (e.detail.value.length == 11) {
        if (!myreg.test(e.detail.value)) {
          Net.toast('手机号输入有误', 'none')
        } else {
          Net.data({
            phone: e.detail.value
          })
        }
      }
      if (e.detail.value.length > 11) {
        Net.toast('手机号长度有误', 'none')
      }
    } else {
      var list = e.detail.value.split('')
      if (list.length > 100) {
        Net.toast('字数已达到上限', 'none')
        var string = ''
        console.log(list)
        for (let i in list) {
          if (i < 100) {
            string = string + list[i]
          }
        }
        Net.data({
          description: string
        })
      } else {
        Net.data({
          description: e.detail.value
        })
      }
    }
  },
  /**
   * 确定方法
   */
  onComest() {
    var _this = this
    var storeId = wx.getStorageSync('store')
    var data = {
      id: storeId
    }
    if (Net.data('pageInput') == 1) {
      if (!Net.data('phone')) {
        Net.toast('请填写手机号', 'none')
        return false
      }
      data.telephone = Net.data('phone')
    } else {
      data.description = Net.data('description')
    }
    Net.link('v1/store/' + storeId, data, function (res) {
      Net.toast('修改成功', 'success')
      _this.onPageInfo()
      Net.data({
        showPageStatus: false
      })
    }, 'PUT')
  },
  /**
   * 上传头像
   */
  onUpDataImg(e) {
    Net.PageRoter('_ceshi', '', 'nav')
  },
  onReady: function () {},
  onShow: function () {
    var _this = this
    var storeId = wx.getStorageSync('store')
    if (_this.data.status == 1) {
      wx.getFileSystemManager().readFile({
        filePath: _this.data.imageUrl, //选择图片返回的相对路径
        encoding: 'base64', //编码格式
        success: rep => { //成功的回调
          Net.link('v1/store/' + storeId, {
            id: storeId,
            logo: 'data:image/png;base64,' + rep.data
          }, function (res) {
            Net.toast('修改成功', 'success')
            _this.onPageInfo()
          }, 'PUT')
        }
      })
    }
  },
  onStatusInput(e) {
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(e.detail.value)) {
      Net.toast('手机号输入有误', 'none')
    } else {
      Net.data({
        phone: e.detail.value
      })
    }
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
})